package com.xinsoft.controller.basic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.entity.po.SysPage;
import com.xinsoft.entity.po.SysPageField;
import com.xinsoft.service.SysPageFieldService;
import com.xinsoft.service.SysPageService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SysPageController
 * @Description 菜单页面控制层
 * @Author lijie
 * @Date 2023年5月18日10:18:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/page")
@Api(value = "SysPageController", tags = {"菜单页面控制层"})
public class SysPageController {
	@Autowired
	private SysPageService pageService;

	@Autowired
	private SysPageFieldService fieldService;
	/**
	 * @Author Li9527
	 * @Description 菜单页面-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@SysLog(title = "菜单页面-新增", businessType = BusinessType.INSERT)
	@ApiOperation("新增")
	@PostMapping("/add")
	public Response<?> add(@RequestBody SysPage page) {
		// 参数校验
		ValidatorUtils.validateEntity(page);
		SysPage oldPage = pageService.getOne(new QueryWrapper<SysPage>()
				.eq("page_code",page.getPageCode())
				.or()
				.eq("page_name",page.getPageName()));
		if(oldPage != null){
			return Response.fail("该页面名称或编码已存在,请不要重复添加");
		}
		return Response.succeed(pageService.save(page));
	}

	/**
	 * @Author Li9527
	 * @Description 更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@SysLog(title = "菜单页面-编辑", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody SysPage page) {
		// 参数校验
		SysPage oldPage = pageService.getOne(new QueryWrapper<SysPage>()
				.eq("page_code",page.getPageCode())
				.or()
				.eq("page_name",page.getPageName()));
		if(oldPage != null && !oldPage.getId().equals(page.getId())){
			return Response.fail("该页面名称或编码已存在,请不要重复添加");
		}
		ValidatorUtils.validateEntity(page);
		return Response.succeed(pageService.updateById(page));
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		return Response.succeed(pageService.getById(id));
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@SysLog(title = "页面管理-删除", businessType = BusinessType.DELETE)
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		pageService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 菜单页面不分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/list")
	public Response<?> list(@RequestBody SysPage sysPage) {
		QueryWrapper<SysPage> queryWrapper = new QueryWrapper<>();
		//查询条件
		if(StrUtil.isNotBlank(sysPage.getPageName())){
			queryWrapper.like("page_name",sysPage.getPageName());
		}
		return Response.succeed(pageService.list(queryWrapper));
	}

	/**
	 * @Author Li9527
	 * @Description 通过菜单id取所有该菜单字段配置
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/listFieldById")
	public Response<?> list(@RequestBody SysPageField pageField) {
		QueryWrapper<SysPageField> queryWrapper = new QueryWrapper<>();
		//查询条件
		if(pageField.getPageId() != null){
			queryWrapper.eq("page_id",pageField.getPageId());
		}
		return Response.succeed(fieldService.list(queryWrapper));
	}

}
